using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
namespace LdExpress
{
    public class CostAccountingDetail
    {
        public long ID = -1;
        public long CostAccountingID = 0;
        public string Description = "";
        public string Units = "";
        public double Amount = 0;
        public string TypeOfCurrency = "";
        public double BuyPrice = 0;
        public double BuyPriceInvoice = 0;
        public double SellPrice = 0;
        public double SellPriceInvoice = 0;
        public double Vat_ComCustomer = 1;
        public string Note = "";

        public CostAccountingDetail()
        {
        }

        public CostAccountingDetail(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_cost_accounting_detail";

            if (ID <= 0) // new object, we insert new record to database
            {
                strSQL = "INSERT INTO " + strTableName +
                    "(CostAccountingID, " +
                    "Description, " +
                    "Units, " +
                    "Amount, " +
                    "TypeOfCurrency, " +
                    "BuyPrice, " +
                    "BuyPriceInvoice, " +
                    "SellPrice, " +
                    "SellPriceInvoice," +
                    "Vat_ComCustomer," +
                    "Note " +
                    ")" +
                    "VALUES" +
                    "(" +
                    CostAccountingID.ToString() + "," +
                    "'" + DBModule.RefineString(Description) + "'," +
                    "'" + DBModule.RefineString(Units) + "'," +
                    Amount.ToString() + "," +
                    "'" + DBModule.RefineString(TypeOfCurrency) + "'," +
                    BuyPrice.ToString() + "," +
                    BuyPriceInvoice.ToString() + "," +
                    SellPrice.ToString() + "," +
                    SellPriceInvoice.ToString() + "," +
                    Vat_ComCustomer.ToString() + "," +
                    "'" + DBModule.RefineString(Note) + "'" +
                    ")";
                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("CostAccountingDetail", "add", cn, trans);
            }
            else // edit object
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                    "CostAccountingID = " + CostAccountingID.ToString() + "," +
                    "Description = '" + DBModule.RefineString(Description) + "'," +
                    "Units = '" + DBModule.RefineString(Units) + "'," +
                    "Amount = " + Amount.ToString() + "," +
                    "TypeOfCurrency = '" + DBModule.RefineString(TypeOfCurrency) + "'," +
                    "BuyPrice = " + BuyPrice.ToString() + "," +
                    "BuyPriceInvoice = " + BuyPriceInvoice.ToString() + "," +
                    "SellPrice = " + SellPrice.ToString() + "," +
                    "SellPriceInvoice = " + SellPriceInvoice.ToString() + "," +
                    "Vat_ComCustomer = " + Vat_ComCustomer.ToString() + "," +
                    "Note = '" + DBModule.RefineString(Note) + "'" +
                    " where ID=" + ID.ToString();
                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("CostAccountingDetail", "edit", cn, trans);
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "DELETE FROM tbl_cost_accounting_detail WHERE ID = " + this.ID.ToString();
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("CostAccountingDetail", "delete", cn, trans);
        }
        public void Delete(string CostAccountingID, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "DELETE FROM tbl_cost_accounting_detail WHERE CostAccountingID = " + CostAccountingID;
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("CostAccountingDetail", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT * FROM tbl_cost_accounting_detail WHERE ID = " + ID.ToString() + " limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("CostAccountingID")) CostAccountingID = long.Parse(dr["CostAccountingID"].ToString());
                if (!dr.IsNull("Description")) Description = dr["Description"].ToString();
                if (!dr.IsNull("Units")) Units = dr["Units"].ToString();
                if (!dr.IsNull("Amount")) Amount = double.Parse(dr["Amount"].ToString());
                if (!dr.IsNull("TypeOfCurrency")) TypeOfCurrency = dr["TypeOfCurrency"].ToString();
                if (!dr.IsNull("BuyPrice")) BuyPrice = double.Parse(dr["BuyPrice"].ToString());
                if (!dr.IsNull("BuyPriceInvoice")) BuyPriceInvoice = double.Parse(dr["BuyPriceInvoice"].ToString());
                if (!dr.IsNull("SellPrice")) SellPrice = double.Parse(dr["SellPrice"].ToString());
                if (!dr.IsNull("SellPriceInvoice")) SellPriceInvoice = double.Parse(dr["SellPriceInvoice"].ToString());
                if (!dr.IsNull("Vat_ComCustomer")) Vat_ComCustomer = double.Parse(dr["Vat_ComCustomer"].ToString());
                if (!dr.IsNull("Note")) Note = dr["Note"].ToString();
            }
        }
        public static void GetList(string strCondition, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_cost_accounting_detail where 1=1 ";
            if ((strCondition != null) && (strCondition != ""))
            {
                strSQL += strCondition;
            }

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL += " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }

        public void DoSearch(string strKeywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_cost_accounting_detail where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
    }
}


